﻿using UnityEngine;
using System.Collections;

public class TowerBloxx : MonoBehaviour
{

	public GameObject jointB;
	public static int floorIndex = 0;
	public int continuity_add, not_add;


	// Use this for initialization
	void Start ()
	{
		floorIndex = 0;
		AddBox ();
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Space)) {
			jointB.GetComponent<Rigidbody> ().AddForce (new Vector3 (0, Random.Range (0, 4.0f), 0), ForceMode.Force);
		}
		if (Input.GetKeyDown (KeyCode.Mouse0)) {
			if (jointB != null) {
				Destroy (jointB.GetComponent<SpringJoint> ());
				jointB.tag = "drop";
			}
		}
		if (Input.GetKeyDown (KeyCode.N)) {
			jointB = (GameObject)Instantiate (Resources.Load ("JointB"), this.transform.position, new Quaternion (0.0f, 0.0f, 0.4f, 0.9f));
			
			jointB.GetComponent<SpringJoint> ().connectedBody = this.GetComponent<Rigidbody> ();
			jointB.GetComponent<SpringJoint> ().connectedAnchor = Vector3.zero;
			jointB.GetComponent<Rigidbody> ().isKinematic = false;
		}
	}

	void con_add ()
	{
		continuity_add++;
		not_add = 0;
		print (continuity_add + "---" + not_add);
	}

	void erro_add ()
	{
		continuity_add = 0;
		not_add++;

	}

	void AddBox ()
	{
	   
		floorIndex++;
		GameObject.Find ("Score").GetComponent<TextMesh> ().text = ":" + floorIndex.ToString ();

		if (continuity_add % 3 == 0 && continuity_add > 0) {
			floorIndex += 1;
			GameObject.Find ("Score").GetComponent<TextMesh> ().text = ":" + floorIndex.ToString ();
		}

		Debug.Log ("Score:" + floorIndex);

		this.transform.Translate (new Vector3 (0, 1.3f, 0), Space.World);
		
		jointB = (GameObject)Instantiate (Resources.Load ("JointB"), this.transform.position, new Quaternion (0.0f, 0.0f, 0.4f, 0.9f));
		GameObject tmp = GameObject.Find ("ImageTarget");
		if (tmp != null) {
			//print ("find imagetarget");
			jointB.transform.parent = tmp.transform;
		}
		jointB.GetComponent<SpringJoint> ().connectedBody = this.GetComponent<Rigidbody> ();
		jointB.GetComponent<SpringJoint> ().connectedAnchor = Vector3.zero;
		
		jointB.GetComponent<Rigidbody> ().isKinematic = false;


	}

	void Restart ()
	{
		//print ("childCount:" + GameObject.Find ("ImageTarget").transform.childCount);



		//print ("childCount1:" + GameObject.Find ("ImageTarget").transform.childCount);
		if (not_add % 2 == 0 && not_add > 0) {
			
			floorIndex -= 1;
			if (GameObject.Find ("ImageTarget").transform.GetChild (GameObject.Find ("ImageTarget").transform.childCount - 2).gameObject.name == "JointB(Clone)") {
				Destroy (GameObject.Find ("ImageTarget").transform.GetChild (GameObject.Find ("ImageTarget").transform.childCount - 2).gameObject);
				this.transform.Translate (new Vector3 (0, -1.3f, 0), Space.World);
			}


			GameObject.Find ("Score").GetComponent<TextMesh> ().text = ":" + floorIndex.ToString ();
		}
		//	GameObject cz = (GameObject)Instantiate (Resources.Load ("43_RFX_Magic_Buff2"), this.transform.position - new Vector3 (0, 7.82f, 0), this.transform.rotation);
		jointB = (GameObject)Instantiate (Resources.Load ("JointB"), this.transform.position, new Quaternion (0.0f, 0.0f, 0.4f, 0.9f));
		GameObject tmp = GameObject.Find ("ImageTarget");
		if (tmp != null) {
			//print ("find imagetarget");
			jointB.transform.parent = tmp.transform;
		}

		jointB.GetComponent<SpringJoint> ().connectedBody = this.GetComponent<Rigidbody> ();
		jointB.GetComponent<SpringJoint> ().connectedAnchor = Vector3.zero;
		jointB.GetComponent<Rigidbody> ().isKinematic = false;
	}
}
